import 'package:fluro/fluro.dart';
import 'package:flutter/material.dart';
import 'package:flutter_picker/flutter_picker.dart';
import 'package:hbtrans_flutter/apis.dart';
import 'package:hbtrans_flutter/const.dart';
import 'package:hbtrans_flutter/routes.dart';
import 'package:hbtrans_flutter/utils/mydio.dart';
import 'package:hbtrans_flutter/utils/utils.dart';
import 'package:hbtrans_flutter/view/loading.dart';
import 'package:shared_preferences/shared_preferences.dart';

class PersonalSet extends StatefulWidget {
  @override
  _PersonalSetState createState() => _PersonalSetState();
}

class _PersonalSetState extends State<PersonalSet> {
  var classes = ['男', '女'];
  String sextype = '请选择性别';
  var _scaffoldKey = GlobalKey<ScaffoldState>();
  Map<String, dynamic> data = Map();
  String phone = '';
  LoadingDialog loadingDialog;

  _showPickerIcons(BuildContext context) {
    Picker(
      adapter: PickerDataAdapter(data: _setPickerItems()),
      title: Text(''),
      cancelText: '取消',
      confirmText: '确定',
      selectedTextStyle: TextStyle(color: Colors.blue),
      onConfirm: (Picker picker, List value) {
        sextype = classes[value[0]];
        data['sex'] = sextype;
        print(sextype);
        setState(() {});
      },
    ).show(_scaffoldKey.currentState);
  }

  _setPickerItems() {
    List<PickerItem<dynamic>> pickerItems = List();
    for (String s in classes) {
      pickerItems.add(PickerItem(
          value: Icons.add,
          text: Text(s,
              style: TextStyle(color: const Color(0xff333333), fontSize: 16))));
    }
    return pickerItems;
  }

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    _getPhone();
  }

  void _getPhone() async {
    SharedPreferences prefs = await SharedPreferences.getInstance();
    phone = await prefs.getString('phone');
    data['phone'] = phone;
    setState(() {});
  }

//  _showDialog() {
//    loadingDialog = LoadingDialog(
//      text: '请稍候...',
//    );
//    showDialog<Null>(
//        context: context,
//        barrierDismissible: false,
//        builder: (BuildContext context) {
//          return loadingDialog;
//        });
//  }

  _userUpdate() async {
//    _showDialog();
    MyDio.getInstance(url: Const.TRANS_SERVER_PATH_NEW).post(Apis.userUpdate,
        (data) {
      print(data);
      if (loadingDialog != null) {
        loadingDialog = null;
        Navigator.pop(context);
      }
      Navigator.pop(context);
    }, params: data);
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      key: _scaffoldKey,
      backgroundColor: Colors.white,
      appBar: AppBar(
        backgroundColor: Color(0xff23AFDC),
        title: Text(
          '个人设置',
        ),
        centerTitle: true,
        actions: <Widget>[
          Center(
              child: GestureDetector(
            onTap: () {
                  if (data['name'] == null || sextype.isEmpty ||
                      phone.isEmpty) {
                    Utils.getInstance().showSnackBar(_scaffoldKey, '请将内容填写完整');
                    return;
                  }
              _userUpdate();
            },
            child: Text(
              '保存',
              style: TextStyle(fontSize: 16, color: Colors.white),
            ),
          ))
        ],
      ),
      body: Padding(
        padding: EdgeInsets.symmetric(horizontal: 20, vertical: 9),
        child: Column(
          children: <Widget>[
            SizedBox(
              height: 50,
              child: Card(
                margin: EdgeInsets.all(0),
                elevation: 0,
                color: Colors.white,
                child: Row(
                  mainAxisAlignment: MainAxisAlignment.spaceBetween,
                  children: <Widget>[
                    Text(
                      '用户名',
                      style: TextStyle(fontSize: 14, color: Color(0xff333333)),
                    ),
                    Container(
                        width: 105,
                        child: TextFormField(
                          style: TextStyle(color: Colors.black, fontSize: 14),
                          onChanged: (value) {
                            value == null
                                ? data['name'] = ''
                                : data['name'] = value;
                          },
                          decoration: InputDecoration(
                            hintText: '请输入用户名',
                            border: InputBorder.none, //去掉下划线
                          ),
                        )),
                  ],
                ),
              ),
            ),
            Container(height: 1, color: Color(0xffF5F5F5)),
            GestureDetector(
              onTap: () {
                _showPickerIcons(context);
              },
              child: SizedBox(
                height: 50,
                child: Card(
                  margin: EdgeInsets.all(0),
                  elevation: 0,
                  color: Colors.white,
                  child: Row(
                    mainAxisAlignment: MainAxisAlignment.spaceBetween,
                    children: <Widget>[
                      Text(
                        '性别',
                        style:
                            TextStyle(fontSize: 14, color: Color(0xff333333)),
                      ),
                      Text(
                        sextype,
                        style:
                            TextStyle(fontSize: 14, color: Color(0xff666666)),
                      ),
                    ],
                  ),
                ),
              ),
            ),
            Container(height: 1, color: Color(0xffF5F5F5)),
            SizedBox(
              height: 50,
              child: Card(
                margin: EdgeInsets.all(0),
                elevation: 0,
                color: Colors.white,
                child: Row(
                  mainAxisAlignment: MainAxisAlignment.spaceBetween,
                  children: <Widget>[
                    Text(
                      '手机',
                      style: TextStyle(fontSize: 14, color: Color(0xff333333)),
                    ),

                    Text(
                      phone==null?'':phone,
                      style: TextStyle(fontSize: 14, color: Color(0xff333333)),
                    ),
                  ],
                ),
              ),
            ),
            Container(height: 8, color: Color(0xffFBFBFB)),
            SizedBox(
              height: 10,
            ),
            GestureDetector(
              onTap: (){
                _onBackPressed();
              },
              child:Container(
                alignment: Alignment.center,
                height: 45,
                child: Text(
                  '退出登录',
                  style: TextStyle(fontSize: 14, color: Color(0xff23AFDC)),
                ),
              ) ,
            )
          ],
        ),
      ),
    );
  }

  Future<bool> _onBackPressed() {
    return showDialog(
        context: context,
        builder: (context) => AlertDialog(
          title: Text('确定退出吗?'),
          elevation: 10,
          shape: RoundedRectangleBorder(
              borderRadius: BorderRadius.circular(5)),
          actions: <Widget>[
            FlatButton(
              child: Text('取消'),
              onPressed: () => Navigator.pop(context, false),
            ),
            FlatButton(
              child: Text('确定'),
              onPressed: () {
                _exit();
              },
            ),
          ],
        ));
  }

  void _exit() async {
    SharedPreferences sharedPreferences = await SharedPreferences.getInstance();
    sharedPreferences.clear();
    Navigator.pop(context);
    setState(() {
      sharedPreferences.setString('token', '');
    });
    String route = '${Routes.register}';
    Routes.router.navigateTo(context, route,
        replace: true, transition: TransitionType.fadeIn);
  }
}
